package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {
    @Autowired
    private DeptService deptService;
    @Autowired
    private EmpExprService empExprService;

    @Autowired
    private EmpMapper empMapper;


    /**
     * 分页查询
     *
     * @param param
     * @param分页参数
     * @return分页返回结果PageResult
     */
    @Override
    public PageResult getPageResult(EmpQueryParam param) {
        Page<Emp> page = new Page<>(param.getPage(), param.getPageSize());
        LambdaQueryWrapper<Emp> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(param.getName()!=null&&param.getName()!="",Emp::getName,param.getName())
                .eq(param.getGender()!=null,Emp::getGender,param.getGender())
                .between(param.getBegin()!=null&&param.getEnd()!=null,Emp::getEntryDate,param.getBegin(),param.getEnd());
        page.addOrder(OrderItem.desc("entry_date"));
        page= page(page,wrapper);
//        page= lambdaQuery().like(param.getName()!=null&&param.getName()!="",Emp::getName,param.getName())
//                .eq(param.getGender()!=null,Emp::getGender,param.getGender())
//                .between(param.getBegin()!=null&&param.getEnd()!=null,Emp::getEntryDate,param.getBegin(),param.getEnd())
//                .page(page);
        List<Emp> emps = page.getRecords();
        List<Long> deptids = emps.stream().map(emp -> emp.getDeptId()).collect(Collectors.toList());

        if(!deptids.isEmpty()){
            List<Dept> depts = deptService.listByIds(deptids);
            emps.forEach(emp -> {
                depts.forEach(dept -> {
                    if(emp.getDeptId()!=null &&  dept.getId().equals(emp.getDeptId())){
                        emp.setDeptName(dept.getName());
                    }
                });
            });
        }

        return new PageResult(page.getTotal(), emps);
    }

    /**
     * 新增员工和经历
     *
     * @param emp
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        save(emp);
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(expr -> {
            expr.setEmpId(emp.getId());
        });
        empExprService.saveBatch(exprList);
    }

    @Override
    public Emp getEmpById(Integer id) {

        Emp emp = empMapper.selectById(id);
        LambdaQueryWrapper<EmpExpr> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EmpExpr::getEmpId,id);
        emp.setExprList(empExprService.list(wrapper));
        return emp;
    }

    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);

        LambdaQueryWrapper<EmpExpr> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EmpExpr::getEmpId,emp.getId());
        empExprService.remove(wrapper);


        List<EmpExpr> exprList = emp.getExprList();
        if(exprList!=null && !exprList.isEmpty()){
            exprList.forEach(expr -> {
                expr.setEmpId(emp.getId());
            });
            empExprService.saveBatch(exprList);
        }
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByIds(List<Integer> ids) {

        removeBatchByIds(ids);
        empExprService.removeBatchByIds(ids);
    }
}
